<template>
  <div id="detail">
    <detail-nav-bar class="navbar" @itemclick="itemclick" ref="nav"/>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info ref="params" :param-info="paramInfo"/>
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list  ref="recommend" :goods="recommends"/>
    </scroll>
    <detail-bottom-bar @addCart="addToCart" class="detail-bottombar"/>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
    <toast :message="message" :show="show"/>
  </div>
</template>

<script>
import Toast from 'components/common/toast/Toast'
import DetailNavBar from './childcops/DetailNavBar'
import DetailSwiper from './childcops/DetailSwiper'
import DetailBaseInfo from './childcops/DetailBaseInfo'
import DetailShopInfo from './childcops/DetailShopInfo'
import DetailGoodsInfo from './childcops/DetailGoodsInfo'
import DetailParamInfo from './childcops/DetailParamInfo'
import DetailCommentInfo from './childcops/DetailCommentInfo'
import DetailBottomBar from './childcops/DetailBottomBar'
import BackTop from 'components/content/backtop/BackTop'
import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'
import { itemListenerMixin } from "common/mixin";
import { debounce } from "common/utils";
import {getDetail, Goods,Shop, GoodsParam, getRecommend} from 'network/detail'
  export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    Scroll,
    DetailBottomBar,
    BackTop,
    Toast
  },
  mixins: [itemListenerMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      currentIndex: 0,
      isShowBackTop: false,
      goodsInfo: {},
      message: '',
      show: false
    }
  },
  created() {
    this.iid = this.$route.params.iid

    getDetail(this.iid).then(res => {
      const data = res.result;
      this.topImages = res.result.itemInfo.topImages

      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      //商店信息
      this.shop = new Shop(data.shopInfo)

      this.detailInfo = data.detailInfo

      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0] || {};
      }
    })

    getRecommend().then(res => {
      this.recommends = res.data.list;
    })
  },
  mounted() {
   
  },
  destroyed() {
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  },
  methods: {
    imageLoad() {
       this.newRefresh()

       
       this.themeTopYs = []

     this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
    },
    itemclick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    contentScroll(position) {
       const positionY = -position.y
    let length = this.themeTopYs.length
    for(let i = 0; i<length; i++) {
      if (this.currentIndex !== i && ((i < length - 1 && positionY > this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) || (i === length - 1 && positionY >= this.themeTopYs[i]))) {
        this.currentIndex = i;
        this.$refs.nav.currentIndex = this.currentIndex
      }
      this.isShowBackTop = (-position.y) > 1000
     }
    },
    backClick() {
    this.$refs.scroll.scrollTo(0,0,500)
    },
    addToCart() {
      const product = {}
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      this.$store.dispatch('addCart', product).then(res => {
        this.show = true;
        this.message = res;

        setTimeout(() => {
          this.show = false;
          this.message = ''
        }, 1500)
      })
    }
  }
}

</script>
 
<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.content {
  height: calc(100% - 44px);
}
.navbar {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
/* vue的淡入淡出动画 */
.scroll-enter-active,
.scroll-leave-active {
  transition: all 0.3s;
}
.scroll-enter,
.scroll-leave-to {
  opacity: 0;
}
.scroll-enter-to,
.scroll-leave {
  opacity: 1;
}

</style>